package com.hk.Mgr.web.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.CardRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.ConstantParm;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Card;
import com.hk.commons.entity.Parameter;
import com.hk.commons.entity.Points;
import com.hk.service.IPointsFunction;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.ICard;
import com.hk.service.kyc.IPoints;
import com.hk.service.kyc.IUserCard;
import com.hk.service.order.IParameter;
import com.hk.service.sys.ISysUser;

/**
 * 用户积分
 * 
 * @author jeff
 *
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/cardsCtr")
public class AppCardsCtrl extends BaseCtrl {

	@Autowired
	private ISysUser userService;

	@Autowired
	private ICard cardService;

	@Autowired
	private IPoints pointsService;

	@Autowired
	private IUserCard userCardService;

	@Autowired
	private IPointsFunction PointsFunctionService;

	@Autowired
	private IUserExtraInfo userExtraService;

	@Autowired
	private IParameter parmService;

	/**
	 * 获取加息券
	 * 
	 * @param userId
	 *            用户id, cardId 加息券id
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public ResponseBase<List<CardRes>> list() {

		ResponseBase<List<CardRes>> resp = new ResponseBase<List<CardRes>>();
		List<CardRes> cardReslist = new ArrayList<CardRes>();
		List<Card> cardlist = new ArrayList<Card>();

		// 首先判断加息券活动是否开始，如果未开始则直接退出
		Parameter parm = parmService.getByName(ConstantParm.CARD_LIST_SHOW);
		if (parm == null || (!parm.getValue01().equals("1"))) {
			resp.setResult(cardReslist);
			resp.setSize(0);
			resp.setResultCode(StatusCode.SUCCESS.key());
			return resp;
		}

		Long nowDate = System.currentTimeMillis();
		HqlFilter hqlFilter = new HqlFilter();

		hqlFilter.addSql(" where startDate <= " + nowDate + " and  endDate >= "
				+ nowDate);
		List<Card> cardTotalList = cardService.findByFilter(hqlFilter);

		cardlist = cardService.findByFilter(hqlFilter);

		for (Card card : cardlist) {
			// 判断加息券是否可以领取
			if (cardService.checkActive(card.getId())) {
				CardRes cardRes = new CardRes();

				ReqToModelHelper.copyReqValueToModel(card, cardRes);
				cardRes.setStartDateStr(DateUtil.longToShortDate(card
						.getStartDate()));
				cardRes.setEndDateStr(DateUtil.longToShortDate(card
						.getEndDate()));
				cardRes.setExpireDateStr(DateUtil.longToShortDate(card
						.getExpireDate()));
				if (card.getPointsRuleId() != null) {
					Points points = pointsService.findById(card
							.getPointsRuleId());
					if (points != null) {
						cardRes.setPointsRuleDesc(points.getId() + " : "
								+ points.getActDesc());
						cardRes.setScore(points.getScore());
					}
				}
				cardReslist.add(cardRes);
			}
		}

		resp.setResult(cardReslist);
		resp.setSize(cardTotalList.size());
		resp.setResultCode(StatusCode.SUCCESS.key());

		return resp;
	}
}
